package com.plian.system.controller.qg.subsidiary;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.constant.sys.FormTypeConstant;
import com.plian.system.entity.qg.subsidiary.MixedOwnershipStructure;
import com.plian.system.entity.qg.subsidiary.SubsidiaryMixedReform;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.qg.subsidiary.IMixedOwnershipStructureService;
import com.plian.system.service.qg.subsidiary.ISubsidiaryMixedReformChangeService;
import com.plian.system.vo.qg.subsidiary.SubsidiaryMixedReformVO;
import com.plian.system.wrapper.qg.subsidiary.MixedOwnershipStructureWrapper;
import com.plian.system.wrapper.qg.subsidiary.SubsidiaryMixedReformWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("api/subsidiarymixedreformchange")
@Api(value = "混改调整信息表", tags = "混改调整信息表接口")
public class SubsidiaryMixedReformChangeController {

    private ISubsidiaryMixedReformChangeService subsidiaryMixedReformChangeService;

    private SubsidiaryMixedReformWrapper subsidiaryMixedReformWrapper;

    private IMixedOwnershipStructureService mixedOwnershipStructureService;

    private MixedOwnershipStructureWrapper mixedOwnershipStructureWrapper;

    /**
     * 详情
     */
    @PostMapping("/detail")
    @ApiOperation(value = "详情", notes = "传入subsidiaryMixedReform")
    public R<SubsidiaryMixedReformVO> detail(@RequestBody SubsidiaryMixedReform subsidiaryMixedReform) {
        SubsidiaryMixedReform detail = subsidiaryMixedReformChangeService.getOne(Condition.getQueryWrapper(subsidiaryMixedReform));
        SubsidiaryMixedReformVO subsidiaryMixedReformVO = subsidiaryMixedReformWrapper.entityToVO(detail);
        subsidiaryMixedReformVO.setMixedOwnershipStructureVOS(mixedOwnershipStructureWrapper.entityToVO(mixedOwnershipStructureService
                .list(new LambdaQueryWrapper<MixedOwnershipStructure>().eq(MixedOwnershipStructure::getMixedReformId, subsidiaryMixedReformVO.getId()))));
        SubsidiaryMixedReformVO lastInfoVO = subsidiaryMixedReformWrapper.entityToVO(subsidiaryMixedReformChangeService.getById(detail.getLastVersionId()));
        lastInfoVO.setMixedOwnershipStructureVOS(mixedOwnershipStructureWrapper.entityToVO(mixedOwnershipStructureService
                .list(new LambdaQueryWrapper<MixedOwnershipStructure>().eq(MixedOwnershipStructure::getMixedReformId, lastInfoVO.getId()))));
        subsidiaryMixedReformVO.setLastInfoVO(lastInfoVO);
        return R.data(subsidiaryMixedReformVO);
    }

    /**
     * 分页 混改变更信息表
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页", notes = "传入query")
    public ListResult<SubsidiaryMixedReformVO> list(@RequestBody Query query) {
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs() == null && query.getDescs() == null) {
            query.setDescs("create_time");
        }
        QueryWrapper<SubsidiaryMixedReform> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new SubsidiaryMixedReform()), entityList
        );
        TokenSearch<SubsidiaryMixedReform> tokenSearch = new TokenSearch<>();
        tokenSearch.addTokenQuery(queryWrapper, query);
        queryWrapper.lambda().eq(SubsidiaryMixedReform::getFormType, String.valueOf(FormTypeConstant.QG_SUBSIDIARY_CHANGE_FORM));

        IPage<SubsidiaryMixedReform> pages = subsidiaryMixedReformChangeService.page(Condition.getPage(query), queryWrapper);
        ListResult<SubsidiaryMixedReformVO> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(subsidiaryMixedReformWrapper.entityToVO(pages.getRecords()));
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());

        return listResult;
    }

    /**
     * 新增或修改 混改变更信息表
     */
    @PostMapping("/submit")
    @LogOperation("混改调整信息-提交")
    @Resubmit(delaySeconds = 0)
    @ApiOperation(value = "新增或修改", notes = "传入subsidiaryMixedReform")
    public R submit(@Valid @RequestBody SubsidiaryMixedReform subsidiaryMixedReform) {
        return R.status(subsidiaryMixedReformChangeService.submit(subsidiaryMixedReform));
    }


    /**
     * 删除 混改变更信息表
     */
    @PostMapping("/remove")
    @LogOperation("混改调整信息-删除")
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>) pd.get("ids");
        return R.status(subsidiaryMixedReformChangeService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA))));
    }

    @GetMapping("/oldRecordInit")
    public R oldRecordInit() {
        subsidiaryMixedReformChangeService.oldRecordInit();
        return R.success();
    }
}
